package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Accumulate;
import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.Record;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.query.MyCollectionQueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.query.UserQuery;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.service.IAccumulateService;
import cn.wolfcode.trip.base.service.IRecordService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IRecordService recordService;
    @Autowired
    private IAccumulateService accumulateService;

    @Autowired
    private IMyCollectionService collectionService;

    @Autowired
    private IStrategyReviewService strategyReviewService;

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult register(User user) {
        JSONResult json = new JSONResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    /**
     * 修改个人信息
     *
     * @param user
     * @return
     */
    @PutMapping
    public JSONResult update(User user) {
        JSONResult json = new JSONResult();
        json.setResult(user);
        try {
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    /**
     * 查询我的游记信息
     *
     * @param qo
     * @return
     */
    @GetMapping("/{authorId}/travels")
    public PageInfo listTravels(TravelQuery qo) {
        //按照最后更新时间倒序
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }

    /**
     * 查询关注和粉丝的数量
     * @return
     */
    @GetMapping("/{id}")
    public Map getFollowAndFansByCount(@PathVariable Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        int fansCount = userService.getFansByCount(id);
        int followCount = userService.getFollowByCount(id);
        map.put("fans",fansCount);
        map.put("follows",followCount);
        return map;

    }

    /**
     * 更新钱包
     *
     * @param user 当前用户名
     * @return
     */
    @PutMapping("/balance")
    public JSONResult updateBalance(User user) {
        JSONResult json = new JSONResult();
        //设置当前用户累计充值
        Accumulate a = new Accumulate();
        a.setUser(user);
        a.setRecharge(user.getBalance());
        //判断添加累计金额添加金额
        Accumulate accumulate = accumulateService.selectOrUser(user.getId());//查询有这个用户在累计添加数据
        if (accumulate == null) {
            accumulateService.saveUser(a);
        }else{
            accumulateService.updateBalanceRecharge(user.getId(),user.getBalance());
        }

        //设置充值记录
        Record record = new Record();
        record.setUser(user);
        record.setState(Record.STATE_TOPUP);
        record.setCashBackTime(new Date());
        record.setMoney(user.getBalance());
        recordService.insert(record);

        userService.updateBalance(user);
        User u = userService.selectByUserId(user.getId());
        json.setResult(u);
        return json;
    }

    /**
     * 查询用户是否有该粉丝
     *
     * @param qo
     * @return
     */
    @GetMapping
    public JSONResult checkFollowAndFans(UserQuery qo) {
        int count = userService.checkFollowAndFans(qo);
        //如果没查到就为0
        if (count <= 0) {
            return new JSONResult().mark(null);
        }
        return new JSONResult();
    }

    // 取消顶回答
    @DeleteMapping("/{uid}/answers/{aid}/dellike")
    public JSONResult dellike(@PathVariable Long uid, @PathVariable Long aid){
        JSONResult jsonResult = new JSONResult();
        answerService.delLike(uid, aid);
        jsonResult.setResult(answerService.getById(aid));
        return jsonResult;
    }

    // 顶回答
    @PutMapping("/{uid}/answers/{aid}/addlike")
    public JSONResult addlike(@PathVariable Long uid, @PathVariable Long aid){
        JSONResult jsonResult = new JSONResult();
        answerService.addLike(uid, aid);
        jsonResult.setResult(answerService.getById(aid));
        return jsonResult;
    }

    // 是否有顶回答
    @GetMapping("/{uid}/answers/{aid}/islike")
    public boolean isLike(@PathVariable Long uid, @PathVariable Long aid){
        Answer answer = answerService.getLikeAnswerById(uid, aid);
        if (answer == null) {
            return false; // 没顶过
        }else {
            return true; // 顶过
        }
    }

    // 取消顶回答
    @DeleteMapping("/{uid}/answers/{aid}/delfav")
    public JSONResult delFav(@PathVariable Long uid, @PathVariable Long aid){
        JSONResult jsonResult = new JSONResult();
        answerService.delFav(uid, aid);
        jsonResult.setResult(answerService.getById(aid));
        return jsonResult;
    }

    // 顶回答
    @PutMapping("/{uid}/answers/{aid}/addfav")
    public JSONResult addFav(@PathVariable Long uid, @PathVariable Long aid){
        JSONResult jsonResult = new JSONResult();
        answerService.addFav(uid, aid);
        jsonResult.setResult(answerService.getById(aid));
        return jsonResult;
    }

    // 是否有顶回答
    @GetMapping("/{uid}/answers/{aid}/isfav")
    public boolean isFav(@PathVariable Long uid, @PathVariable Long aid){
        Answer answer = answerService.getFavAnswerById(uid, aid);
        if (answer == null) {
            return false; // 没顶过
        }else {
            return true; // 顶过
        }
    }

    // 取消关注问题
    @DeleteMapping("/{uid}/questoins/{qid}/delfollow")
    public JSONResult delFollow(@PathVariable Long uid, @PathVariable Long qid){
        questionService.delFollow(uid, qid);
        return new JSONResult();
    }

    // 关注问题
    @PutMapping("/{uid}/questoins/{qid}/addfollow")
    public JSONResult addFollow(@PathVariable Long uid, @PathVariable Long qid){
        questionService.addFollow(uid, qid);
        return new JSONResult();
    }

    // 是否有关注问题
    @GetMapping("/{uid}/questoins/{qid}/isfollow")
    public boolean isFollow(@PathVariable Long uid, @PathVariable Long qid){
        Question question = questionService.getFollowQuestionById(uid, qid);
        if (question == null) {
            return false; // 没顶过
        }else {
            return true; // 顶过
        }
    }

    /**
     * 查询用户所有的收藏
     * @param qo
     * @return
     */
    @RequestMapping(value = "/{userId}/myCollections", method = RequestMethod.GET)
    public PageInfo queryCollectionsByUserId(MyCollectionQueryObject qo) {
        return collectionService.query(qo);
    }

    // 取消赞攻略点评
    @DeleteMapping("/{uid}/sreviews/{sid}/delsfav")
    public JSONResult delSFav(@PathVariable Long uid, @PathVariable Long sid){
        strategyReviewService.delSFav(uid, sid);
        return new JSONResult();
    }

    // 赞攻略点评
    @PutMapping("/{uid}/sreviews/{sid}/addsfav")
    public JSONResult addSFav(@PathVariable Long uid, @PathVariable Long sid){
        strategyReviewService.addSFav(uid, sid);
        return new JSONResult();
    }

    // 是否有赞攻略点评
    @GetMapping("/{uid}/sreviews/{sid}/issfav")
    public boolean isSFav(@PathVariable Long uid, @PathVariable Long sid){
        StrategyReview strategyReview = strategyReviewService.getFavSRById(uid, sid);
        if (strategyReview == null) {
            return false; // 没顶过
        }else {
            return true; // 顶过
        }
    }
}
